//
//  LWMainTabBarVC.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/19.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
import SVProgressHUD
class LWMainTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //方案一:统一设置tabBar的选中颜色
//      UITabBar.appearance().tintColor = UIColor.orange
        
        //使用KVC给OC中的类设置只读属性的值
//      self.tabBar = LWTabBar()
        let lwTabbar = LWTabBar()
        //设置代理对象
        lwTabbar.lwDelegate = self
        
        lwTabbar.composeButtonClosure = {
            [weak self] in
            print(self)
            print("我是闭包调用过来的")

            if !LWUserAccountViewModel.sharedUserAccoutnViewModel.isLogin {
    
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            
            //  代码执行到此表示已经登录
            let composeView = LWComposeView()
            //  最上层的window
            let window = UIApplication.shared.windows.last!
            window.addSubview(composeView)

        }
        
        
        setValue(lwTabbar, forKey: "tabBar")
        
        
        addChildViewController(childController: LWHomeTabVC(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: LWMessageTabVC(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: LWDiscoverTabVC(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: LWProfileTabVC(), title: "我的", imageName: "tabbar_profile")
        
    }

    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        
//        方案二:选中的效果图
        childController.tabBarItem.image = UIImage(imageLiteralResourceName: imageName)
        //图片的渲染方式
        childController.tabBarItem.selectedImage = UIImage(imageLiteralResourceName: "\(imageName)_selected").withRenderingMode(.alwaysOriginal)
        //文字的选中颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //字体的大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
        
        // 标题
        childController.title = title
        //导航控制器视图
//        let nav = UINavigationController(rootViewController: childController)
        let nav = LWNavVC(rootViewController: childController)
        //添加子视图
        addChildViewController(nav)
    }
}

//使用extension可以实现代理方法
extension LWMainTabBarVC :LWTabBarDelegate{
    
    func didSelectedComposeButton() {
        print("我是代理对象调用的")
    }
    
}
